
package com.hualan.sfzy.service.content.serviceimpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.content.Article;
import com.hualan.sfzy.entity.content.ArticleClassify;
import com.hualan.sfzy.mapper.content.ArticleClassifyMapper;
import com.hualan.sfzy.mapper.content.ArticleMapper;
import com.hualan.sfzy.service.content.ArticleClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@Transactional

@Service("articleClassifyService")
public class ArticleClassifyServiceImpl implements ArticleClassifyService {
    @Autowired
    private ArticleClassifyMapper mapper;
    @Autowired
    private ArticleMapper articleMapper;

    public  int add(ArticleClassify classify){
        return mapper.insert(classify);
    }
    //不能直接删除，若关联了文章表
    public int remove(Integer id){
        Article article=new Article();
        article.setArticleClassifyId(id);
        List<Article> list= articleMapper.select(article);
        if(!list.isEmpty()){
            throw new RuntimeException("存在关联文章，不能删除");
        }
        return mapper.delete(id);
    }
    //由于分类较少，前端直接把批量删除按钮扣掉了
    public int removeBatch(Integer[] ids){
        Article article=new Article();
        for (Integer id : ids) {
            article.setArticleClassifyId(id);
            List<Article> list= articleMapper.select(article);
            if(!list.isEmpty()){
                throw new RuntimeException("存在关联文章，不能删除");
            }
        }
        return mapper.deleteBatch(ids);
    }
    public int edit(ArticleClassify classify){
        return mapper.update(classify);
    }
    public ArticleClassify findById(Integer id){
        return mapper.selectById(id);
    }
    public List<ArticleClassify> find(){
        return mapper.select(null);
    }

    public Map<String,Object> findByPage(Integer pageNum, Integer pageSize,ArticleClassify classify){
        System.out.println(pageNum+" "+pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleClassify> list = mapper.select(classify);
        PageInfo<ArticleClassify> pageInfo = new PageInfo<>(list);
        Map<String , Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;

    }
}
